package com.liser.southernstar.model.mapper;

import com.liser.southernstar.collection.model.dto.GoodsFamilyDTO;
import com.liser.southernstar.collection.model.dto.GoodsItemDTO;
import com.liser.southernstar.collection.model.vo.GoodsGroupedItem2;
import com.liser.southernstar.collection.model.vo.OrderItemVO;
import com.liser.southernstar.collection.model.vo.OrderTypeVO;
import com.liser.southernstar.model.bean.GoodsPrintDetails;
import com.liser.southernstar.model.bean.GoodsPrintOverview;
import com.liser.southernstar.model.vo.GoodsGroupedHeader;
import com.liser.southernstar.model.vo.GoodsGroupedItem;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2020/1/10 15:58
 */
public class ConversionFactory {

    public static OrderTypeVO orderTypeVO(GoodsFamilyDTO goodsFamilyDTO) {
        OrderTypeVO orderTypeVO = new OrderTypeVO();
        orderTypeVO.setId(goodsFamilyDTO.getId());
        orderTypeVO.setName(goodsFamilyDTO.getName());
        orderTypeVO.setBiz_name(goodsFamilyDTO.getBiz_name());
        orderTypeVO.setSort(goodsFamilyDTO.getSort());
        return orderTypeVO;
    }

    public static OrderItemVO orderItemVO(GoodsItemDTO goodsItemDTO) {
        OrderItemVO orderItemVO = new OrderItemVO();
        orderItemVO.setFk_biz(goodsItemDTO.getFk_biz());
        orderItemVO.setBiz_name(goodsItemDTO.getBiz_name());
        orderItemVO.setFk_genre(goodsItemDTO.getFk_genre());
        orderItemVO.setGenre_name(goodsItemDTO.getGenre_name());
        orderItemVO.setMain_img(goodsItemDTO.getMain_img());
        orderItemVO.setFigure(goodsItemDTO.getFigure());
        orderItemVO.setId(goodsItemDTO.getId());
        orderItemVO.setName(goodsItemDTO.getName());
        orderItemVO.setDescribe(goodsItemDTO.getDescribe());
        orderItemVO.setDiscount(goodsItemDTO.getDiscount());
        orderItemVO.setPrice(goodsItemDTO.getPrice());
        return orderItemVO;
    }

    public static List<GoodsGroupedItem2> goodsGroupedItem(GoodsFamilyDTO goodsFamilyDTO) {
        List<GoodsGroupedItem2> goodsGroupedItemList = new ArrayList<>();
        String group = goodsFamilyDTO.getName();
        GoodsGroupedItem2 goodsGroupedItem = new GoodsGroupedItem2(true, group);
        goodsGroupedItemList.add(goodsGroupedItem);
        List<GoodsItemDTO> goodsList = goodsFamilyDTO.getGoodsList();

        for (GoodsItemDTO goodsItemDTO : goodsList) {
            GoodsGroupedItem2 goodsGroupedItemInner = new GoodsGroupedItem2(false, group);
            GoodsGroupedItem2.ItemInfo itemInfo = new GoodsGroupedItem2.ItemInfo(goodsItemDTO.getName(), group);
            itemInfo.setFk_biz(goodsItemDTO.getFk_biz());
            itemInfo.setBiz_name(goodsItemDTO.getBiz_name());
            itemInfo.setFk_genre(goodsItemDTO.getFk_genre());
            itemInfo.setGenre_name(goodsItemDTO.getGenre_name());
            itemInfo.setMain_img(goodsItemDTO.getMain_img());
            itemInfo.setFigure(goodsItemDTO.getFigure());
            itemInfo.setId(goodsItemDTO.getId());
            itemInfo.setName(goodsItemDTO.getName());
            itemInfo.setDescribe(goodsItemDTO.getDescribe());
            itemInfo.setDiscount(goodsItemDTO.getDiscount());
            itemInfo.setPrice(goodsItemDTO.getPrice());

            goodsGroupedItemInner.info = itemInfo;
            goodsGroupedItemList.add(goodsGroupedItemInner);
        }
        return goodsGroupedItemList;
    }

    public static List<GoodsGroupedHeader> goodsGroupedHeader(GoodsFamilyDTO goodsFamilyDTO) {
        List<GoodsGroupedHeader> goodsGroupedHeaderList = new ArrayList<>();
        String group = goodsFamilyDTO.getName();
        GoodsGroupedHeader goodsGroupedHeader = new GoodsGroupedHeader(true, group);
        goodsGroupedHeaderList.add(goodsGroupedHeader);
        // 头部
        List<GoodsItemDTO> goodsItemDTOList = goodsFamilyDTO.getGoodsList();

        for (GoodsItemDTO goodsItemDTO : goodsItemDTOList) {
            // 列表项头部
            GoodsGroupedHeader headerInner = new GoodsGroupedHeader(false, "");
            // 列表项内容
            GoodsGroupedItem itemInner = new GoodsGroupedItem(goodsItemDTO.getName(), group);
            itemInner.setFk_biz(goodsItemDTO.getFk_biz());
            itemInner.setBiz_name(goodsItemDTO.getBiz_name());
            itemInner.setFk_genre(goodsItemDTO.getFk_genre());
            itemInner.setGenre_name(goodsItemDTO.getGenre_name());
            itemInner.setMain_img(goodsItemDTO.getMain_img());
            itemInner.setFigure(goodsItemDTO.getFigure());
            itemInner.setId(goodsItemDTO.getId());
            itemInner.setName(goodsItemDTO.getName());
            itemInner.setDescribe(goodsItemDTO.getDescribe());
            itemInner.setDiscount(goodsItemDTO.getDiscount());
            itemInner.setPrice(goodsItemDTO.getPrice());

            headerInner.info = itemInner;
            goodsGroupedHeaderList.add(headerInner);
        }
        return goodsGroupedHeaderList;
    }

    public static GoodsPrintOverview goodsPrintOverview(List<GoodsGroupedItem> goodsGroupedItemList) {
        BigDecimal amount = new BigDecimal(0.0);
        int total = 0;

        List<GoodsPrintDetails> goodsPrintDetailsList = new ArrayList<>(goodsGroupedItemList.size());
        for (GoodsGroupedItem goodsGroupedItem : goodsGroupedItemList) {
            amount = amount.add(goodsGroupedItem.getPrice()).multiply(BigDecimal.valueOf(goodsGroupedItem.getSelectCount()));
            total = total + goodsGroupedItem.getSelectCount();

            GoodsPrintDetails goodsPrintDetails = new GoodsPrintDetails();
            goodsPrintDetails.setGoodsName(goodsGroupedItem.getName());
            goodsPrintDetails.setGoodsPrice(goodsGroupedItem.getPrice().toString());
            goodsPrintDetails.setGoodsNum(String.valueOf(goodsGroupedItem.getSelectCount()));
            goodsPrintDetails.setGoodsUnit("份");
            goodsPrintDetails.setGoodsAmount(amount.toString());
            goodsPrintDetailsList.add(goodsPrintDetails);
        }

        GoodsPrintOverview goodsPrintOverview = new GoodsPrintOverview();
        goodsPrintOverview.setExpressSn("2020123456789");
        goodsPrintOverview.setSubsidy("0.00");
        goodsPrintOverview.setGoodsAmount(amount.toString());
        goodsPrintOverview.setOrderAmount(String.valueOf(total));
        goodsPrintOverview.setGoodsList(goodsPrintDetailsList);
        return goodsPrintOverview;
    }
}
